/*
    自定义配置
*/

function merge(dst, src) {

    /*
        对象属性merge
     */

    let res = dst;

    if (typeof dst == "object" && typeof src == "object") {
        let keys = Object.keys(src)
        keys.forEach(key => {
            dst[key] = src[key]
        })
    }

    return res
}

export function parseSetting(cfg, options) {
    /*
        分析产品配置，定制页面内容
     */
    cfg = cfg || {};
    options = options || {};
    let pageSettings = cfg.pageSettings || {};
    let resultPageSettings = pageSettings.result || {};
    let confirmModal = {
        title: "结果正在分析中",
        content: "看一段小视频，马上解锁结果~",
        showCancel: true,
        confirmText: "看广告获取结果",
        showCancelExceptFor: [], // 每天第几次作图显示取消按钮，showCancel=false时有效
        showResultForCancel: false, // 点击取消以后是否立即给结果图
    }

    let videoAd = {
        watchCountLimitEnable: false,
        watchCountLimit: 0,
    }

    if (resultPageSettings.confirmModal) {
        confirmModal = merge(confirmModal, resultPageSettings.confirmModal)
        if (confirmModal &&
            confirmModal.showCancel == false &&
            confirmModal.showCancelExceptFor &&
            Array.isArray(confirmModal.showCancelExceptFor)) {
            // 根据访问次数决定cancel按钮是否可见
            let showCancelExceptFor = confirmModal.showCancelExceptFor;
            let resultPagePv = options.resultPagePv || 0;
            if (showCancelExceptFor.indexOf(resultPagePv) != -1) {
                confirmModal.showCancel = true;
            }
        }
    }

    if (resultPageSettings.videoAd) {
        videoAd = merge(videoAd, resultPageSettings.videoAd)
    }

    return {
        videoAd: videoAd,
        confirmModal: confirmModal,
    };
};